.jet_common:
  stage: jet
  rules:
    - if: $CI_PIPELINE_SOURCE == "merge_request_event" && $CI_MERGE_REQUEST_APPROVED 
    - if: '$CI_PIPELINE_SOURCE == "merge_request_event" && $CI_MERGE_REQUEST_LABELS =~ /READY FOR REVIEW/'
    - if: $JET_CUSTOM_FILTER != "" && $CI_PIPELINE_SOURCE != 'merge_request_event'
    - when: never

include:
  - project: dl/jet/gitlab-templates
    ref: main
    file: downstreams.yml

jet-setup:
  extends: [ .jet_common ]
  tags: 
    - os/linux
  script:
    - set -x
    - |
      if [[ $CI_PIPELINE_SOURCE == "merge_request_event" ]] && [[ $CI_MERGE_REQUEST_APPROVED || $CI_MERGE_REQUEST_LABELS =~ "READY FOR REVIEW" ]]; then
          JET_FILTER="type == 'build' or 'merge-request' in spec.scope"
      elif [[ -n $JET_CUSTOM_FILTER && $CI_PIPELINE_SOURCE != 'merge_request_event' ]]; then
        JET_FILTER=$JET_CUSTOM_FILTER
      else
        JET_FILTER="False"
      fi
      echo "_JET_FILTER=$JET_FILTER" | tee -a config.env
  artifacts:
    reports:
      dotenv: config.env

jet-configure:
  extends: [.jet_common, .jet-configure]
  tags:
    - os/linux
  script:
    - wget https://github.com/mikefarah/yq/releases/download/v4.35.2/yq_linux_amd64.tar.gz -O - | tar xz && mv yq_linux_amd64 /usr/local/bin/yq
    - cd tests/functional_tests/jet_recipes
    - |
      if [[ $CI_PIPELINE_SOURCE == "merge_request_event" ]]; then
        yq e ".spec.source.ref = \"merge-requests/${CI_MERGE_REQUEST_IID}/head\"" -i build-pyt.yaml
      else
        yq e ".spec.source.ref = \"${CI_COMMIT_REF_NAME}\"" -i build-pyt.yaml
      fi
  artifacts:
    paths:
      - tests/functional_tests/jet_recipes

jet-trigger:
  stage: jet
  extends: [.jet_common, .jet-trigger]
  needs:  [ jet-configure, jet-setup ]
  trigger:
    project: dl/jet/ci
    branch: mcore/eos
    strategy: depend
  inherit:
    variables:
      - JET_CUSTOM_FILTER
  variables:
    JET_WORKLOADS_FILTER: "$_JET_FILTER"


jet-functional-results:
  stage: jet
  tags:
    - docker_local_runner
  image: gitlab-master.nvidia.com:5005/dl/jet/api:latest
  needs: [ jet-trigger ]
  before_script:
    - jet secrets jwt-login jwt/nvidia/gitlab-master adlr-megatron-lm-ci $CI_JOB_JWT
  script: 
    - python -m pip install -U --no-cache-dir prettytable
    - python tests/functional_tests/python_test_utils/jet_test_pipeline.py ${CI_PIPELINE_ID} --test exit
  rules:
    - if: $CI_PIPELINE_SOURCE == "merge_request_event" && $CI_MERGE_REQUEST_APPROVED 
      when: always
    - if: '$CI_PIPELINE_SOURCE == "merge_request_event" && $CI_MERGE_REQUEST_LABELS =~ /READY FOR REVIEW/'
      when: always
    - if: $JET_CUSTOM_FILTER != "" && $CI_PIPELINE_SOURCE != 'merge_request_event'
      when: always
    - when: never

jet-compare-metrics:
  extends: .jet_common
  tags:
    - docker_local_runner
  image: gitlab-master.nvidia.com:5005/dl/jet/api:latest
  needs: [ jet-functional-results ]
  before_script:
    - jet secrets jwt-login jwt/nvidia/gitlab-master adlr-megatron-lm-ci $CI_JOB_JWT
  script:
    - python -m pip install -U --no-cache-dir pytest tensorboard
    - python tests/functional_tests/python_test_utils/jet_test_pipeline.py ${CI_PIPELINE_ID} --test metrics
